package flow.action;

import flow.model.FlowSection;
import flow.model.FlowSectionLink;
import flow.service.FlowSectionService;
import m.common.action.ActionMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.element.other.ItemsQuery;
import m.html.list.*;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.action.ManageAction;
import manage.model.AdminGroup;

@ActionMeta(name="flow/flowSection")
public class FlowSectionAction extends ManageAction {
	private FlowSection model;
	private FlowSectionLink link;

	public JSONMessage doSave(){
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_flow_power");
			String msg=getService(FlowSectionService.class).save(model);
			fillJSONResult(result,true,msg);
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public JSONMessage doDelete(){
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_flow_power");
			getService(FlowSectionService.class).delete(model);
			fillJSONResult(result,true,"删除成功");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public JSONMessage doVerify(){
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_flow_power");
			getService(FlowSectionService.class).verifySection(model.getFlowDefine().getOid());
			fillJSONResult(result,true,"验证通过");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public JSONMessage doSaveNext(){
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_flow_power");
			String msg=getService(FlowSectionService.class).saveNext(link);
			fillJSONResult(result,true,msg);
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public JSONMessage doDeleteNext(){
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_flow_power");
			getService(FlowSectionService.class).deleteNext(link);
			fillJSONResult(result,true,"删除成功");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public VueResult toEdit() throws Exception {
		verifyAdminOperPower("manage_flow_power");
		if(null!=model&&!StringUtil.isSpace(model.getOid())){
			model=ModelQueryList.getModel(model,"*");
		}else if(null!=model&&null!=model.getFlowDefine()&&!StringUtil.isSpace(model.getFlowDefine().getOid())){
			model.setCountersign("N");
			model.setForwardable("N");
		}else {
			throw new MException(this.getClass(), "参数错误");
		}
		return new VuePageResult("page/base/flow/editFlowSection.js")
			.addParam("model",model);
	}
	public VueResult toEditNext() throws Exception {
		verifyAdminOperPower("manage_flow_power");
		if(null!=link&&!StringUtil.isSpace(link.getOid())){
			link=ModelQueryList.getModel(link,"*","fromSection.*");
		}else if(null!=link&&null!=link.getFromSection()&&!StringUtil.isSpace(link.getFromSection().getOid())){
			link.setFromSection(ModelQueryList.getModel(link.getFromSection(), "*"));
			link.setIsNext("N");
			link.setToOrgOption("N");
		}else {
			throw new MException(this.getClass(), "参数错误");
		}
		return new VuePageResult("page/base/flow/editFlowSectionLink.js")
			.addParam("link",link)
			.addParam("nextList",
				new ItemsQuery(FlowSection.class,"name","oid")
				.setTitleExpression("concat(name,' (',identity,')')")
				.addCond(
					QueryCondition.eq("flowDefine.oid", link.getFromSection().getFlowDefine().getOid()),
					QueryCondition.not(QueryCondition.eq("oid", link.getFromSection().getOid()))
				)
				.toDatas()
			);
	}
	public VueResult flowSectionList() throws Exception{
		boolean isEdit= getParamsValue("oper").equals("edit");
		boolean power=hasAdminOperPower("manage_flow_power");
		VueListResult result=getVueListResult("流程环节",FlowSectionLink.class,"action/flow/flowSection/flowSectionList");
		result.setSubTitle("添加或者编辑流程信息。");
		result.setMergeCellsInfo(0,7);
		result.addOrder(QueryOrder.asc("fromSection.sort"), QueryOrder.asc("isNext"));
		//result.addCol(toIndexColumn().setFixed(Align.LEFT));
		result.addCol(toTextColumn("组", "fromSection.block",50).setFixed(Align.LEFT));
		result.addCol(toTextColumn("排序", "fromSection.sort",50).setFixed(Align.LEFT));
		result.addCol(toTextColumn("标识", "fromSection.identity",50).setFixed(Align.LEFT));
		result.addCol(toTextColumn("名称", "fromSection.name",100).setFixed(Align.LEFT));
		result.addCol(toTextColumn("转发", "fromSection.forwardable",50).setDictOption(FlowSection.class,"forwardable"));
		result.addCol(toTextColumn("会签", "fromSection.countersign",50).setDictOption(FlowSection.class,"countersign"));
		if(power&&isEdit) result.addCol(toSpaceColumn("操作",
			ListDropdown.primary("操作").add(
				ListButton.modal("修改",ButtonType.PRIMARY,"action/flow/flowSection/toEdit",800,SuccessMethod.REFRESH)
					.addParam("model.oid","fromSection.oid"),
				ListButton.ajax("删除", ButtonType.ERROR, "action/flow/flowSection/doDelete", SuccessMethod.REFRESH)
					.addParam("model.oid", "fromSection.oid")
					.setConfirm("确定要删除该环节和所有环节链接吗?"),
				ListButton.modal("添加环节链接",ButtonType.PRIMARY,"action/flow/flowSection/toEditNext",600,SuccessMethod.REFRESH)
					.addParam("link.fromSection.oid","fromSection.oid")
			)
		));
		result.addCol(toTextColumn("下一步", "isNext",60).setDictOption(FlowSectionLink.class,"isNext"));
		result.addCol(toTextColumn("环节", "toSection.name",100));
		result.addCol(toTextColumn("参数", "toParam",60));
		result.addCol(toTextColumn("描述", "toDesc"));
		result.addCol(toTextColumn("选项", "toOption",80).setDictOption(FlowSectionLink.class,"toOption"));
		result.addCol(toTextColumn("部门限制", "toOrgOption",80).setDictOption(FlowSectionLink.class,"toOrgOption"));
		result.addCol(toTextColumn("可选部门", "toOrg",100).setDictType("manage.model.AdminGroup:C"));
		result.addCol(toTextColumn("角色限制", "toRole",100).setDictType("manage.model.AdminGroup:B"));

		if(power&&isEdit) result.addCol(toSpaceColumn("操作",
			ListDropdown.primary("操作").add(
				ListButton.modal("修改",ButtonType.PRIMARY,"action/flow/flowSection/toEditNext",600,SuccessMethod.REFRESH)
					.addParam("link.oid","oid"),
				ListButton.ajax("删除", ButtonType.ERROR, "action/flow/flowSection/doDeleteNext", SuccessMethod.REFRESH)
					.addParam("link.oid", "oid")
					.setConfirm("确定要删除该环节链接吗?")
			)
		).setFixed(Align.RIGHT));
		if(power&&isEdit) result.addButton(
			ListButton.modal("新增环节",ButtonType.PRIMARY,"action/flow/flowSection/toEdit",800,SuccessMethod.REFRESH)
				.addQueryParam("model.flowDefine.oid", "fromSection.flowDefine.oid"),
			ListButton.ajax("验证环节",ButtonType.SUCCESS,"action/flow/flowSection/doVerify")
				.addQueryParam("model.flowDefine.oid", "fromSection.flowDefine.oid")
		);
		result.addButton(
			ListButton.modal("流程图",ButtonType.DEFAULT,"page/base/flow/viewFlowDefine.js",800)
				.addQueryParam("defineOid", "fromSection.flowDefine.oid")
		);
		result.addSearch(ListSearch.field("","fromSection.flowDefine.oid",SearchType.HIDDEN_EQ));
		return result;
	}

	public FlowSection getModel() {
		return model;
	}

	public void setModel(FlowSection model) {
		this.model = model;
	}

	public FlowSectionLink getLink() {
		return link;
	}

	public void setLink(FlowSectionLink link) {
		this.link = link;
	}
	
	
}
